# -*- coding: utf-8 -*-
import scrapy
from quotetutorial.items import QuoteItem


class QuotesSpider(scrapy.Spider):
	name = "quotes"
	allowed_domains = ["quotes.toscrape.com"]
	start_urls = ['http://quotes.toscrape.com/']

	def parse(self, response):
		quotes = response.css('.quote')
		for quote in quotes:
			item = QuoteItem()  # 实例化items.py下的类
			text = quote.css('.text::text').extract_first()
			author = quote.css('.author::text').extract_first()
			tags = quote.css('.tags .tag::text').extract()
			# 对实例进行赋值
			item['text'] = text
			item['author'] = author
			item['tags'] = tags
			yield item  # 生成item

		# 获取下一页的url
		next = response.css('.pager .next a::attr(href)').extract_first(())
		# 由于获取的下一页url不是绝对值，所以进行绝对url操作
		page_url = response.urljoin(next)
		# 对下一页进行回调处理
		yield scrapy.Request(url=page_url, callback=self.parse)

	def start_requests(self):
		pass
